package com.netty.cn.rpc.string.client;

import com.netty.cn.rpc.string.RpcStringClient;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 
 * @author wangzonghui
 * @date 2021-07-19 16:37:26
 * @Description 客户端handler
 */
public class StringClientHandler extends ChannelInboundHandlerAdapter {
	
	private RpcStringClient rpcStringClient;
	
	public StringClientHandler() {
		this.rpcStringClient=null;
	}
	
	
	public StringClientHandler(RpcStringClient rpcStringClient) {
		this.rpcStringClient=rpcStringClient;
	}
	
	/**
     * 连接
     */
	@Override
   public void channelActive(ChannelHandlerContext ctx) {
		System.out.println("连接服务端 "+ctx.channel().remoteAddress()+" 成功");
   }
	
	/**
	 * 接收消息
	 */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("client 接收到信息:"+msg.toString());
    }
    
    /**
     * 关闭重连
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    	super.channelInactive(ctx);
    	//重连
    	if(rpcStringClient!=null) {
    		rpcStringClient.doConnect();
    	}
    }
}
